package com.lagou.rpc.consumer;

import com.lagou.rpc.consumer.client.RpcClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

@SpringBootApplication
public class ConsumerApp implements CommandLineRunner {

    @Autowired
    private RpcClient rpcClient;

    private int index=0;


    private static List<String> list=new ArrayList<>();
    static {
        list.add("127.0.0.1:8899");
        list.add("127.0.0.1:8898");
        list.add("127.0.0.1:8897");
    }

    public static void main(String[] args) {

        SpringApplication.run(ConsumerApp.class,args);
    }

    @Override
    public void run(String... args) throws Exception {
//        map.get(i);
        String[] inetAddress = getInetAddress();
        System.out.println("服务端信息:"+inetAddress);
        try {
            rpcClient.initClient(inetAddress[0], Integer.parseInt(inetAddress[1]));
        }catch (NumberFormatException e){
            e.printStackTrace();
        }
    }

    private String[] getInetAddress(){
        Random r=new Random();
//        int i = r.nextInt(3);
        int size = list.size();
        index=index % size;
        String s1 = list.get(index);
        index++;
        //根据不同算法取ip port
        return s1.split(":");
    }
}
